#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdkhooks>

#define PLUGIN_VERSION "1.3.2"

public Plugin myinfo =
{
    name = "[L4D/L4D2]TankDamageModifier",
    author = "Lux",
    description = "简单的坦克伤害修改",
    version = PLUGIN_VERSION,
    url = "https://forums.alliedmods.net/showthread.php?p=2421778#post2421778"
};

ConVar hCvar_DmgEnable, hCvar_Damage, hCvar_IncapMulti;

int ZOMBIECLASS_TANK, g_DmgEnable;

float g_iDamage, g_iImultiplyer;

public void OnPluginStart()
{
	char sGameName[12];
	GetGameFolderName(sGameName, sizeof(sGameName));
	if(StrEqual(sGameName, "left4dead"))
		ZOMBIECLASS_TANK = 5;
	else if(StrEqual(sGameName, "left4dead2"))
		ZOMBIECLASS_TANK = 8;
	else
		SetFailState("This plugin only runs on Left 4 Dead and Left 4 Dead 2!");

	RegAdminCmd("sm_tank", Command_TankSwitch, ADMFLAG_KICK, "坦克伤害指令开关");

	CreateConVar("tank_damage_modifier_lvt", PLUGIN_VERSION, "插件版本", FCVAR_NOTIFY);

	hCvar_DmgEnable = CreateConVar("tank_damage_enable", "1", "是否启用坦克伤害修改插件", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	hCvar_Damage = CreateConVar("tank_damage", "1.0", "坦克攻击生还者造成多少伤害", FCVAR_NOTIFY, true, 0.0);
	hCvar_IncapMulti = CreateConVar("tank_damage_modifier", "1.0", "对倒地的生还者造成多少倍数的伤害(设置的伤害*倒地时设定的伤害倍数)", FCVAR_NOTIFY, true, 0.0);
	
	HookConVarChange(hCvar_DmgEnable, eConvarChanged);
	HookConVarChange(hCvar_Damage, eConvarChanged);
	HookConVarChange(hCvar_IncapMulti, eConvarChanged);

	//AutoExecConfig(true, "l4d2_Tank_Damage_Modifier");
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	g_DmgEnable = hCvar_DmgEnable.IntValue;
	g_iDamage = hCvar_Damage.FloatValue;
	g_iImultiplyer = hCvar_IncapMulti.FloatValue;
}

public Action Command_TankSwitch(int client, int args)
{
	if (g_DmgEnable)
	{
		hCvar_DmgEnable.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x05坦克伤害插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_DmgEnable.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x05坦克伤害插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

public void OnClientPutInServer(int client)
{
	SDKHook(client, SDKHook_OnTakeDamage, eOnTakeDamage);
}

public Action eOnTakeDamage(int iVictim, int& iAttacker, int& iInflictor, float& fDamage, int& iDamagetype)
{
	if(g_DmgEnable == 0)
		return Plugin_Continue;
   
	if(!IsClientInGame(iVictim) || GetClientTeam(iVictim) != 2)
		return Plugin_Continue;
       
	if(iAttacker < 1 || iAttacker > MaxClients)
		return Plugin_Continue;
		
	if(GetClientTeam(iAttacker) != 3 || GetEntProp(iAttacker, Prop_Send, "m_zombieClass") != ZOMBIECLASS_TANK)
		return Plugin_Continue;
	
	static char sInflictor[18];
	GetEntityClassname(iInflictor, sInflictor, sizeof(sInflictor));
	
	if(sInflictor[0] == 'p' && StrContains(sInflictor, "prop") > 0 )// should work for all props however if the classname is changed to something else, maybe i should check the net class instead
		return Plugin_Continue;
	
	if(IsSurvivorIncapacitated(iVictim))
	{
		fDamage = (g_iDamage * g_iImultiplyer);
		return Plugin_Changed;
	}
	else
	{
		fDamage = g_iDamage;// supports point hurt entity for realish tank physx
		return Plugin_Changed;
	}
}

bool IsSurvivorIncapacitated(int client)
{
	return GetEntProp(client, Prop_Send, "m_isIncapacitated", 1) > 0;
}